from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.generics import GenericAPIView
from rest_framework.response import Response
from util.paginator import AllGenericPagination

from . import models
from . import serializer

# Create your views here.


#班级详细表
class ClassMateDetailGenericAPIView(GenericAPIView):
    queryset = models.ClassMateDetail.objects.all()
    serializer_class = serializer.ClassMateDetailModelSerializer

    def post(self,request):
        ser = self.get_serializer(data=request.data)
        if ser.is_valid():
            ser.save()
            return Response({'code': 200, 'data': ser.data})
        else:
            return Response({'code': 400, 'error': ser.errors})


#老师创建
class TeacherGenericAPIView(GenericAPIView):
    queryset = models.TeacherModel.objects.all()
    serializer_class = serializer.TeacherModelSerializer

    def get(self,request):
        ser = self.get_serializer(instance = self.get_queryset(),many=True)
        return Response({'code':200,'data':ser.data})
    def post(self, request):
        ser = self.get_serializer(data=request.data)
        if ser.is_valid():
            ser.save()
            return Response({'code': 200, 'data': ser.data})
        else:
            return Response({'code': 400, 'error': ser.errors})


#班级创建
class ClassMateGenericAPIView(GenericAPIView):
    authentication_classes = []
    queryset = models.ClassMateModel.objects.all()
    serializer_class = serializer.ClassMateModelSerializer
    def get(self,request):
        ser = self.get_serializer(instance = self.get_queryset(),many=True)
        return Response({'code':200,'data':ser.data})
    def post(self,request):
        ser = self.get_serializer(data=request.data)
        if ser.is_valid():
            ser.save()
            return Response({'code':200,'data':ser.data})
        else:
            return Response({'code':400,'error':ser.errors})
    def put(self,request):
        obj = models.ClassMateModel.objects.first()
        print(obj.name,obj.detail.all())
        return Response({'code':200,'msg':'222'})

class StudentPKGenericAPIView(GenericAPIView):
    serializer_class = serializer.StudentModelSerializer
    queryset = models.StudentModel.objects.all()
    def get(self,pk):
        user = self.get_object()
        ser = self.get_serializer(instance=user)
        self.get_queryset().filter()
        self.queryset.filter()
        return Response(data=ser.data)
#学生创建
class StudentGenericAPIView(GenericAPIView):
    authentication_classes = []
    queryset = models.StudentModel.objects.all()
    serializer_class = serializer.StudentModelSerializer
    def get(self,request):
        from util.model_to_iterable import model_to_list
        ser = self.get_serializer(instance = self.get_queryset(),many=True)
        # print(request.query_params.get('page'),'------------')
        students = models.StudentModel.objects.all()
        # print(type(students))
        stu_list = model_to_list(students,default={'sex':'男','class':'5年一班'},replace={'name':'student_name'})

        paginator = AllGenericPagination(stu_list, request)

        data = paginator.start()
        return Response(data)

    def post(self, request):
        ser = self.get_serializer(data=request.data)
        if ser.is_valid():
            ser.save()
            return Response({'code': 200, 'data': ser.data})
        else:
            return Response({'code': 400, 'error': ser.errors})